//
//  NSAttributedString+Extension.swift
//  Weibo
//
//  Created by teacher on 2017/6/16.
//  Copyright © 2017年 teacher. All rights reserved.
//

import UIKit

extension NSAttributedString {
    
    //   根据表情模型创建富文本
    class func attributedStringWithEmoticon(emoticon: HMEmoticon, font: UIFont) -> NSAttributedString {
        //  1. 创建UIImage对象
        let image = UIImage(named: emoticon.path!, in: EmoticonButton, compatibleWith: nil)
        //  2. 创建文本附件(NSTextAttachment)
        let attachment = HMEmoticonTextAttachment()
        //  文本附件关联表情模型
        attachment.emoticon = emoticon
        //  获取文字的高度
        let lineHeight = font.lineHeight
        //  设置bounds， 设置文本附件的bounds会影响image的显示位置，在父子视图里面，设置父视图的bounds会影响子视图的显示位置
        attachment.bounds = CGRect(x: 0, y: -4, width: lineHeight, height: lineHeight)
        //  3. 设置文本附件对应的图片对象
        attachment.image = image
        //  4. 根据文本附件创建富文本
        let emoticonAttributeText = NSAttributedString(attachment: attachment)
        return emoticonAttributeText
    }
    
}
